const execel2json = require("./execel2json.js")
const path = require('path')

function uuid() {
    return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (c) {
        var r = Math.random() * 16 | 0,
            v = c == 'x' ? r : (r & 0x3 | 0x8);
        return v.toString(16);
    });
}

const knex = require('knex')({
    client: 'mysql',
    connection: {
        host: 'localhost',
        user: 'root',
        port: "3306",
        password: '123123123',
        database: '123123123'
    }
});

async function init() {
    // var json = await execel2json(path.resolve(__dirname, "./dist/关键词检索（顾）(1).xlsx"), "展商分类")
    // var json = await execel2json(path.resolve(__dirname, "./dist/展商.xlsx"), "1111")

    /*    var json1 = require('./json1.json')
        var json2 = require('./json2.json')
        var json3 = [];
        json1.map(item => {
            var item2 = json2.filter(item2 => item2.展商名称 == item.公司名)[0]
            if (item2) {
                item.大类 = item2.大类
                item.分类 = item2.分类
            }
            json3.push(item)
        })*/
    /*
    * 得到其中一个demo one
    * 然后复制或者修改
    *
    * */
    var json = require('./json3.json')
    var oneDemo = await knex('supplier').first("*").where({
        projectId: '3f8ad648-c7ca-486c-9ec5-c562cc3d13e0',
        status: 0
    })
    for (let i = 0; i < json.length; i++) {
        var insertOne = json[i]
        var one = await knex('supplier').first("*").where({
            projectId: '3f8ad648-c7ca-486c-9ec5-c562cc3d13e0',
            company: insertOne.公司名
        })
        // var tags="";

        //修改
        var company = insertOne.公司名
        var boothNumber = insertOne.展位号
        if (boothNumber) {
            boothNumber = boothNumber.split("：") && boothNumber.split("：")[1]
        }
        var tagsStr = ""
        var tagsArray = []
        if (insertOne.大类 && insertOne.大类.trim()) {
            var _bigOne = await knex('tb_dict_detail').first("id").where({
                chinese: insertOne.大类.trim()
            })
            if (_bigOne.id) {
                tagsArray.push(_bigOne.id)
            }
        }
        if (insertOne.分类 && insertOne.分类.trim()) {
            var _smallOne = await knex('tb_dict_detail').first("id").where({
                chinese: insertOne.分类.trim()
            })
            if (_smallOne.id) {
                tagsArray.push(_smallOne.id)
            }
        }
        tagsStr = tagsArray.join(",")

        var mobilePhone = insertOne['联系人手机号']||''
        var accountName = insertOne['所属人']||'';
        var userName = insertOne['所属人']||'';
        var contact = insertOne['所属人']||'';
        var introduction = insertOne['关键词摘取']||'';
        if (!mobilePhone) {
            continue;
        }
        if (one && one.id) {
            //update
            one.company = company;
            one.boothNumber = boothNumber;
            one.tags = tagsStr;
            one.accountName = accountName;
            one.contact = accountName;
            one.userName = userName;
            one.mobilePhone=mobilePhone;
            one.introduction = introduction;
                 var udpateIndex = await knex('supplier').where({id:one.id}).update({
                     projectId: '3f8ad648-c7ca-486c-9ec5-c562cc3d13e0',
                     ...one
                 })
            console.log("udpate one", i,udpateIndex, {id: one.id, company: one.company})
        } else {
//insert
            oneDemo.id = uuid()
            oneDemo.company = company;
            oneDemo.boothNumber = boothNumber;
            oneDemo.tags = tagsStr;
            oneDemo.accountName = accountName;
            oneDemo.userName = userName;
            oneDemo.introduction = introduction;
            oneDemo.mobilePhone=mobilePhone;
            oneDemo.contact = accountName;
              var addIndex = await knex('supplier').insert({
                  projectId: '3f8ad648-c7ca-486c-9ec5-c562cc3d13e0',
                  ...oneDemo
              })
            console.log("one addIndex", i,addIndex, {id: oneDemo.id, company: oneDemo.company})

        }

    }

    console.log("导入结束")
}

init()
